<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *会员列表
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
);

//会员列表
if($_RQ['op']=='list'){
	$params=array('merchid'=>$_SESSION['onemerchid']);
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['name'])){
		$params['name like']='%'.trim($_RQ['name']).'%';
		$return['params']['name']=$_RQ['name'];
	}
	if(!empty($_RQ['tag_id'])){
		$params['tags like']='%'.trim($_RQ['tag_id']).'%';
		$return['params']['tag_id']=$_RQ['tag_id'];
	}
	if(!empty($_RQ['remark'])){
		$params['remark like']='%'.trim($_RQ['remark']).'%';
		$return['params']['remark']=$_RQ['remark'];
	}
	if(!empty($_RQ['remark_company'])){
		$params['remark_company like']='%'.trim($_RQ['remark_company']).'%';
		$return['params']['remark_company']=$_RQ['remark_company'];
	}
	if(!empty($_RQ['remark_mobiles'])){
		$params['remark_mobiles like']='%'.trim($_RQ['remark_mobiles']).'%';
		$return['params']['remark_mobiles']=$_RQ['remark_mobiles'];
	}
	if(!empty($_RQ['workerid'])){
		$params['workerid']=intval($_RQ['workerid']);
		$return['params']['workerid']=$_RQ['workerid'];
	}
	if(!empty($_RQ['mobile'])){
		$params['mobile']=$_RQ['mobile'];
		$return['params']['mobile']=$_RQ['mobile'];
	}
	if(!empty($_RQ['gender'])){
		$params['gender']=intval($_RQ['gender']);
		$return['params']['gender']=$_RQ['gender'];
	}
	if(!empty($_RQ['qunid'])){
		$params['qunid']=intval($_RQ['qunid']);
		$return['params']['qunid']=$_RQ['qunid'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
		$return['params']['createtime']=$time_range;
	}
	$return['tags']=pdo_getall('jy_scrm_member_group_tag',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']),array('tag_id','title'),'tag_id');
	$list=pdo_getslice('jy_scrm_member',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			if(!$item['workerid']){
				$item['workerid']=model('jy_scrm/worker')->get_workerid($item['follow_user_userid']?:$item['follow_user_oper_userid']);
				pdo_update('jy_scrm_member',array('workerid'=>$item['workerid']),array('id'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));		
			}
			$mobile=$params['follow_user'] && $item['follow_user']['remark_mobiles']?$item['follow_user']['remark_mobiles'][0]:'';
			$item['mobile']=$item['mobile']?:$mobile;
			$item['member']=array('nickname'=>$item['name'],'avatar'=>$item['avatar'],'mobile'=>$item['mobile']);
			$item['tags']=json_decode($item['tags'],true);
			$item['tagstitle']='';
			foreach($item['tags'] as $tv){
				if($item['tagstitle']){
					$item['tagstitle'] .=';'.$tv['tag_name'];
				}else{
					$item['tagstitle']=$tv['tag_name'];
				}
			}
			$item['midmember']=cfc('core')->member_show($item['mid']);
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：1、成员在外部会话中，可通过侧边栏打开【客户详情页】页面，也可以点击外部联系人（客户）头像进入到系统页面【客户信息】，再打开【客户详情页】页面。</br>
		2、在职员工/已离职员工所持有的客户数分配走后，在客户列表中不再显示原归属员工的客户。</br>
		3、如何判断粉丝是否为企业微信的外部联系人？</br>
		4、为了避免销售人员长时间沟通无果或是恶意保护占有客户资源，在达到客户回收条件时，需要将企微客户分配/共享给其他员工，进行跟踪，从而提高跟进效率，有效利用客户资源，则在【客户公海】--【企微客户】列表里生成一条该企微客户数据。
		</br>5、<red>企业微信客户跟公众号粉丝互通</red>？需要开启微信三方开放平台：https://open.weixin.qq.com/，<red>如何设置？如何绑定微信开发者ID?</red>：https://developer.work.weixin.qq.com/document/path/92114#%E5%A6%82%E4%BD%95%E7%BB%91%E5%AE%9A%E5%BE%AE%E4%BF%A1%E5%BC%80%E5%8F%91%E8%80%85id
		
		','edit'=>true,'del_one'=>true,'delall'=>true,'list'=>array(
			array('name'=>'id','title'=>'ID','type'=>'string'),
			array('name'=>'member','title'=>'基本信息','type'=>'member'),
			array('name'=>'midmember','title'=>'系统会员','type'=>'member'),
			array('name'=>'pcd','title'=>'区域','type'=>'string'),
			array('name'=>'groupid','title'=>'分组','type'=>'select','options'=>'groups'),
			array('name'=>'tagstitle','title'=>'标签','type'=>'string'),
			array('name'=>'comefrom','title'=>'来源','type'=>'radio','options'=>'user_froms'),
			array('name'=>'workerid','title'=>'归属成员','type'=>'select','options'=>'workers','option_title'=>'name'),
			array('name'=>'buystatus','title'=>'跟进状态','type'=>'radio','options'=>'user_buystatus'),
			array('name'=>'createtime','title'=>'注册时间','type'=>'time'),
		),'batch'=>array(
			//array('type'=>'modal','url'=>'jy_scrm/member.list/settag/','title'=>'打标签'),
			//array('type'=>'post','url'=>'jy_scrm/member.list/celtag/','title'=>'移除标签'),
			array('type'=>'post','url'=>'jy_merchscrm/merch.member.list/upmember/','title'=>'同步所有客户'),
			array('type'=>'post','url'=>'jy_merchscrm/merch.member.list/upmembernew/','title'=>'同步新增客户'),
			//array('type'=>'model','url'=>'jy_scrm/member.list/seemember/','title'=>'查看重复客户'),
		),'action'=>array(
			array('type'=>'modal','op'=>'settag','title'=>'打标签'),
			array('type'=>'modal','op'=>'look','title'=>'备注信息','show'=>array('type'=>1)),
			array('type'=>'href','op'=>'bindmember','title'=>'绑定系统会员'),
		)),
		'search'=>array(
			array('name'=>'name','title'=>'客户昵称','type'=>'string','width'=>'10'),
			array('name'=>'mobile','title'=>'手机号','type'=>'string','width'=>'10'),
			array('name'=>'tag_id','title'=>'标签','type'=>'select','options'=>'tags','option_value'=>'tag_id','width'=>'10'),
			array('name'=>'remark','title'=>'备注','type'=>'string','width'=>'10'),
			array('name'=>'remark_company','title'=>'备注公司','type'=>'string','width'=>'10'),
			array('name'=>'remark_mobiles','title'=>'备注电话','type'=>'string','width'=>'10'),
			array('name'=>'buystatus','title'=>'跟进状态','type'=>'radio','options'=>'user_buystatus','width'=>'15'),
			array('name'=>'sex','title'=>'客户性别','type'=>'radio','options'=>'sex','width'=>'15'),
			array('name'=>'isfans','title'=>'是否粉丝','type'=>'radio','options'=>'isfans','width'=>'15'),
			array('name'=>'comefrom','title'=>'客户来源','type'=>'radio','options'=>'user_froms','width'=>'15'),
			array('name'=>'qunid','title'=>'选择群聊','type'=>'select','options'=>'quns','width'=>'15'),
			array('type'=>'choose','width'=>15,'title'=>'部门成员','name'=>'workerid','action'=>'jy_scrm/worker.get_worker/'),
			array('name'=>'createtime','width'=>15,'title'=>'添加时间','type'=>'time_range'),
		),	
	);
	$return['user_buystatus']=$this->user_buystatus;
	$return['sex']=array('未知','男','女');
	$return['isfans']=array('否','是');
	$return['user_froms']=$this->user_froms;
	$return['workers']=pdo_getall('jy_scrm_worker',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']),array('id','name'),'id');
	$return['quns']=pdo_getall('jy_scrm_qun',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['groups']=pdo_getall('jy_scrm_member_group',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_scrm_member',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$item['birthtime']=date('Y-m-d',$item['birthtime']);
		$item['jiehun']=date('Y-m-d',$item['jiehun']);
		$item['buyday']=date('Y-m-d',$item['buyday']);
	}else{
		$item=array();
	}
	$return['item']=$item;
	$return['item']['createtime']=date('Y-m-d H:i:s',$item['createtime']);
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回会员列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('type'=>'string','title'=>'会员简称','name'=>'nickname','help'=>''),
					array('type'=>'choose','name'=>'mid','title'=>'绑定的会员','choose_title'=>'选择系统会员','help'=>'绑定后，企业微信前端，可以获取系统会员相关数据（订单等）','action'=>'core/member.get_member/'),
					array('type'=>'image','title'=>'头像','name'=>'avatar','help'=>''),
					array('type'=>'select','title'=>'会员组','name'=>'groupid','options'=>'group','help'=>''),
					array('type'=>'select','title'=>'会员等级','name'=>'level','options'=>'levels','help'=>''),
					array('type'=>'string','title'=>'真实姓名','name'=>'realname','help'=>''),
					array('type'=>'string','title'=>'手机号','name'=>'mobile','help'=>''),
					array('name'=>'remark','title'=>'备注','type'=>'string'),
					array('name'=>'remark_company','title'=>'备注公司','type'=>'string'),
					array('name'=>'remark_mobiles','title'=>'备注电话','type'=>'string'),
					array('name'=>'remark_position','title'=>'备注职称','type'=>'string'),
					array('type'=>'time','title'=>'客户生日','name'=>'birthtime'),
					array('type'=>'time','title'=>'结婚纪念日','name'=>'jiehun'),
					array('type'=>'time','title'=>'首次下单日','name'=>'buyday'),
					array('type'=>'string','title'=>'注册时间','name'=>'createtime','help'=>'','read'=>true),
					array('type'=>'switch','title'=>'加入黑名单','name'=>'isblack','help'=>'设置黑名单后，此会员无法访问商城'),
				)
			),
				array(
				'title'=>'交易信息',
				'list'=>array(
					array('type'=>'string','title'=>'成交订单数','name'=>'order_count','help'=>'','read'=>true),
					array('type'=>'string','title'=>'成交额','name'=>'order_money','help'=>'','read'=>true),
				)
			),
		)
	);
	$return['group']=pdo_getall('jy_scrm_member_group',array('uniacid'=>$_SESSION['uniacid']));
	$return['levels']=pdo_getall('jy_scrm_member_level',array('uniacid'=>$_SESSION['uniacid']));
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'mid'=>intval($postdata['mid']),
		'nickname'=>$postdata['nickname'],
		'avatar'=>$postdata['avatar'],
		'realname'=>$postdata['realname'],
		'mobile'=>$postdata['mobile'],
		'groupid'=>intval($postdata['groupid']),
		'level'=>intval($postdata['level']),
		'isblack'=>intval($postdata['isblack']),
		'remark'=>$postdata['remark'],
		'remark_company'=>$postdata['remark_company'],
		'remark_mobiles'=>$postdata['remark_mobiles'],
		'remark_position'=>$postdata['remark_position'],
		'birthtime'=>strtotime($postdata['birthtime']),
		'jiehun'=>strtotime($postdata['jiehun']),
		'buyday'=>strtotime($postdata['buyday']),
	);
	if($data['birthtime']){
		$data['birth']=date('Y-m-d',$data['birthtime']);
	}
	if($data['jiehun']){
		$data['jiehunday']=date('Y-m-d',$data['jiehun']);
	}
	if($data['buyday']){
		$data['buyday2']=date('Y-m-d',$data['buyday']);
	}
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_scrm_member',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['merchid']=$_SESSION['onemerchid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('jy_scrm_member',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='look'){
	$item=pdo_get('jy_scrm_member',array('id'=>intval($_RQ['id'])));
	if(!$item){
		exi('指定用户不存在！','error');
	}
	$item['birthtime']=$item['birthtime']?date('Y-m-d',$item['birthtime']):'无';
	$item['jiehun']=$item['jiehun']?date('Y-m-d',$item['jiehun']):'无';
	$item['buyday']=$item['buyday']?date('Y-m-d',$item['buyday']):'无';
	if($item['buydaygoods']){
		$item['buyday'].='购买:'.$item['buydaygoods'];
	}
	$item['buydays']=$item['buydays']?json_decode($item['buydays'],true):array();
	$buydays='';
	foreach($item['buydays'] as $bv){
		$bv['goods']=$bv['goods']?:'无';
		$buydays .=$bv['year'].'-'.$bv['month'].'-'.$bv['day'].'购买:'.$bv['goods'].';';
	}
	$item['buydays']=$buydays?:'无';
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'look',
		'desc'=>'备注信息',
		'look'=>array(
			array('type'=>'string','title'=>'备注信息','name'=>'remark'),
			array('type'=>'string','title'=>'备注公司','name'=>'remark_company'),
			array('type'=>'string','title'=>'备注电话','name'=>'remark_mobiles'),
			array('type'=>'string','title'=>'客户生日','name'=>'birthtime'),
			array('type'=>'string','title'=>'结婚纪念日','name'=>'jiehun'),
			array('type'=>'string','title'=>'首次下单日','name'=>'buyday'),
			array('type'=>'string','title'=>'其他下单日','name'=>'buydays'),
		),
	);
	exi($return);
}

if($_RQ['op']=='settag'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=$post['id'];
		$add_tag=str_array($post['addtags']);
		if(empty($add_tag)){
			exi('请选择标签！','error');
		}
		$post['tags']=$post['tags']?json_decode($post['tags'],true):array();
		$membertags=array();
		$newadd_tag=array();
		foreach($add_tag as $av){
			$taginfo=pdo_get('jy_scrm_member_group_tag',array('merchid'=>$_SESSION['onemerchid'],'tag_id'=>$av,'uniacid'=>$_SESSION['uniacid']));
			$nowtag=array(
				'tag_name'=>$taginfo['title'],
				'tag_id'=>$av,
				'userid'=>$post['follow_user_userid'],
			);
			$newadd_tag[]=$av;
			$membertags[]=$nowtag;
			$post['tags'][]=$nowtag;
		}
		if($id){
				pdo_update('jy_scrm_member',array('tags'=>ijson_encode($post['tags'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
				$member=pdo_get('jy_scrm_member',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
				$params=array(
					'userid'=>$member['follow_user_userid'],
					'external_userid'=>$member['external_userid'],
					'add_tag'=>$newadd_tag,
				);
				if($member['follow_user_userid'] && $member['external_userid']){
					model('core/wxwork_userapi')->mark_member_tags($params);
				}
		}
		exi('设置成功！','','//list/');
	}
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择用户','error');
	}
	$return['item']=pdo_get('jy_scrm_member',array('id'=>intval($_RQ['id'])));
	$htmls=array(
		'type'=>'edit',
		'desc'=>'设置标签',
	);
	$htmls['edit']=array(
		array('type'=>'checkbox_select','title'=>'标签','name'=>'addtags','options'=>'tags','option_value'=>'tag_id','help'=>'选择《客户标签》里面已同步的标签'),
	);
	$return['tags']=pdo_getall('jy_scrm_member_group_tag',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']),array('tag_id','title'),'tag_id');
	$return['htmls']=$htmls;
	exi($return);
}

if($_RQ['op']=='bindmember'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=$post['id'];
		if(empty($post['mid'])){
			exi('请选择会员！','error');
		}
		if($id){
			pdo_update('jy_scrm_member',array('mid'=>$post['mid']),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
		exi('绑定成功！','','//list/');
	}
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择用户','error');
	}
	$return['item']=pdo_get('jy_scrm_member',array('id'=>intval($_RQ['id'])));
	$htmls=array(
		'type'=>'edit',
		'desc'=>'绑定会员',
	);
	$htmls['edit']=array(
		array('type'=>'choose','name'=>'mid','title'=>'绑定的会员','choose_title'=>'选择系统会员','help'=>'绑定后，企业微信前端，可以获取系统会员相关数据（订单等）','action'=>'core/member.get_member/'),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='celtag'){
	$ids=trim($_RQ['ids'],';');
	if($ids){
		$ids=explode(';',$ids);
	}else{
		$ids=array();
	}
	if($_RQ['id']){
		$ids[]=$_RQ['id'];
	}
	if(count($ids)==0){
		exi('请先选择用户','error');
	}
	foreach($ids as $id){
		$id=intval($id);
		if($id){
				pdo_update('jy_scrm_member',array('tags'=>''),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('移除成功！','','//list/');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_scrm_member',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择会员！','error');
	}
	$post=get_postdata();
	if(isset($post['isblack'])){
		pdo_update('jy_scrm_member',array('isblack'=>intval($post['isblack'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
if($_RQ['op']=='upmember'){
	model('jy_scrm/model')->check_apilog('get_batch_member',true);
	$workers=pdo_getall('jy_scrm_worker',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'status'=>1),array('id','userid'));
	if(count($workers)<=0){
		exi('请先同步通讯录！','error');
	}
	$this->session('workers',$workers);
	exi('开始同步，请勿关闭页面！','','/merch.member.list/touserid/');
}
if($_RQ['op']=='upmembernew'){
	model('jy_scrm/model')->check_apilog('get_batch_member',true);
	$workers=pdo_getall('jy_scrm_worker',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'status'=>1),array('id','userid'));
	if(count($workers)<=0){
		exi('请先同步通讯录！','error');
	}
	$this->session('workers',$workers);
	exi('开始新增同步，请勿关闭页面！','','jy_merchscrm/merch.member.list/touserid/isnew=1');
}
if($_RQ['op']=='touserid'){
	if($this->session('userid_key')){
		$userid_key=$this->session('userid_key');
	}else{
		$userid_key=0;
	}
	$workers=$this->session('workers');
	$userid=$workers[$userid_key]['userid'];
	if($userid){
		model('jy_scrm/member')->batch($userid,100,'',$_RQ['isnew']);
	}
	if($userid_key+1>=count($workers)){
		model('jy_scrm/model')->add_apilog('get_batch_member');
		$this->session('userid_key',0);
		exi('同步客户成功！','','jy_merchscrm/merch.member.list/');
	}
	$this->session('userid_key',$userid_key+1);
	exi('请勿关闭页面，继续同步下一个成员的好友！','','/merch.member.list/touserid/');
}